package com.itheima.service.impl;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.exception.AuthenticationFailedException;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.LoginDTO;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 短信验证码登录
     * @param loginDTO
     */
    @Override
    public void login(LoginDTO loginDTO) {
        // 判断验证码是否正确
        String phone = loginDTO.getTelephone();
        String code = redisTemplate.opsForValue().get(phone + "Login");
        if (!Objects.equals(code, loginDTO.getValidateCode())){
            throw new AuthenticationFailedException(MessageConstant.VALIDATECODE_ERROR);
        }
        // 移除redis缓存
        redisTemplate.delete(phone + "Login");
    }
}
